package com.wb.repository;

import com.wb.entity.JpaTestBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository(value = "jpaTestDao")
public interface JpaTestDao extends JpaRepository<JpaTestBean, Integer> {
    
    /**
     *  @author: 神经刀
     *  @Date: 2019/6/12 13:12
     *  @Description: 根据ID查询
     */
    Integer countById(Integer id);

    /**
     *  @author: 神经刀
     *  @Date: 2019/6/12 13:13
     *  @Description: 查询jpa集合
     */
    @Query(value = "select jt.id,jt.jpaName,jt.jpaAge from JpaTestBean jt where jt.id = ?1",
          countQuery = "select count(*) from JpaTestBean jt where jt.id = ?1")
    Page<JpaTestBean> findById(int id , Pageable pageable);

    /**
     *  @author: 神经刀
     *  @Date: 2019/6/12 13:13
     *  @Description: 根据对象查询jpa集合
     */
    @Query(value = "select new com.wb.entity.JpaTestBean(jt.id,jt.jpaName,jt.jpaAge) from JpaTestBean jt where jt.jpaName like :#{#jpaTestBean.jpaName}%",
            countQuery = "select count(*) from JpaTestBean jt where jt.jpaName like :#{#jpaTestBean.jpaName}%")
    Page<JpaTestBean> findByJpName(@Param("jpaTestBean") JpaTestBean jpaTestBean , Pageable pageable);

    @Query(nativeQuery = true,
    value = "select * from jpa_test jt where jt.id = :#{#jpaTestBean.id}")
    List<JpaTestBean> findByJpaIdOnSql(@Param("jpaTestBean") JpaTestBean jpaTestBean);
}